package com.tyg.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tyg.entity.ExercisesInfo;
import com.tyg.service.ExercisesInfoService;
import com.tyg.util.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.EmptyStackException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangjingnan
 * @since 2022-11-05
 */
@RestController
@RequestMapping("/exercises-info")
@CrossOrigin
@Slf4j
public class ExercisesInfoController {

    @Autowired
    ExercisesInfoService exercisesInfoService;

    //封面查询
    @PostMapping("/getExerciseList")
    public ResultCode getExerciseList(@RequestParam(defaultValue = "1" ,required = true)int pageIndex, int pageSize) throws Exception{
        try {
            PageHelper.startPage(pageIndex, pageSize);
            List<ExercisesInfo> exerciseList=exercisesInfoService.getExerciseList();
            PageInfo<ExercisesInfo> page=new PageInfo<ExercisesInfo>(exerciseList);
            return ResultCode.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "查询失败！请联系服务人员");
        }
    }


    @PostMapping("/lookExercise")
    public ResultCode lookExercise(Integer exercisesId) throws Exception {
        try {
            ExercisesInfo exercisesInfo = exercisesInfoService.selectById(exercisesId);
            return ResultCode.success(exercisesInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "查询失败！请联系服务人员");
        }

    }

    //模糊查询用
    @PostMapping("/getExercise")
    public ResultCode getExercise(
            String exercisesName,
            Integer status,
            @RequestParam(defaultValue = "1" ,required = true)int pageIndex, int pageSize){
        PageHelper.startPage(pageIndex, pageSize);
        Map<String,Object> map= new HashMap<>();
        map.put("exercisesName", exercisesName);
        map.put("status", status);
        List<ExercisesInfo> exerciseList=exercisesInfoService.getExercise(map);
        PageInfo<ExercisesInfo> page=new PageInfo<ExercisesInfo>(exerciseList);
        return ResultCode.success(page);
    }

    @PostMapping("/addExercise")
    public ResultCode addExercise(ExercisesInfo exercisesInfo, MultipartFile exercisePicture, HttpServletRequest request)throws IllegalStateException, IOException {
        try {
            System.out.println(exercisesInfo);
            if(!exercisePicture.isEmpty()) {
                String path = request.getServletContext().getRealPath("/upload_images/exercise");
                File targetFile= new File(path,exercisePicture.getOriginalFilename());
                exercisePicture.transferTo(targetFile);
                String basePath="http://localhost:8080/TYG_sys/upload_images/exercise/";
                exercisesInfo.setPhoto(basePath + exercisePicture.getOriginalFilename());
            }
            exercisesInfo.setStatus(1);
            exercisesInfoService.insert(exercisesInfo);
            return ResultCode.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "添加失败！");
        }
    }

    @PostMapping("/getExercisesById")
    public ResultCode getExercisesById(Integer exercisesId) {
        System.out.println(exercisesId);
        try {
            ExercisesInfo exercisesInfo= exercisesInfoService.selectById(exercisesId);
            return ResultCode.success(exercisesInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "查询失败！请联系服务人员");
        }
    }

    @PostMapping("/upLoad")
    public ResultCode updExercise(Integer exercisesId,MultipartFile exercisePicture, HttpServletRequest request)throws IllegalStateException, IOException {
        try {
            System.out.println(exercisesId);
            ExercisesInfo exercisesInfo=exercisesInfoService.selectById(exercisesId);
            System.out.println(exercisesInfo);
            if(!exercisePicture.isEmpty()) {
                String path = request.getServletContext().getRealPath("/upload_images/exercise");
                File targetFile= new File(path,exercisePicture.getOriginalFilename());
                exercisePicture.transferTo(targetFile);
                String basePath="http://localhost:8080/TYG_sys/upload_images/exercise/";
                exercisesInfo.setPhoto(basePath + exercisePicture.getOriginalFilename());
            }
            exercisesInfo.setStatus(1);
            exercisesInfoService.updateById(exercisesInfo);
            return ResultCode.success();
        }
        catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "添加失败！");
        }
    }

    @PostMapping("/updForm")
    public ResultCode updExercise(@RequestBody ExercisesInfo exercisesInfo){
        System.out.println(exercisesInfo);
        try {
            Integer idOld=exercisesInfo.getExercisesId();
            int result=exercisesInfoService.updateByOld(exercisesInfo);
            return ResultCode.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "查询失败！请联系服务人员");
        }

    }

    @PostMapping("/retract")
    public ResultCode retract(Integer exercisesId){
        ExercisesInfo exercisesInfo=exercisesInfoService.retract(exercisesId);
        return ResultCode.success(exercisesInfo);
    }

}

